<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>tab 栏切换</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      li {
        list-style: none;
      }
      .tab-box {
        width: 800px;
        margin: 100px auto 0;
      }

      .nav {
        height: 40px;
        line-height: 38px;
        background-color: #eee;
        border: 1px solid #c8cac7;
        box-sizing: border-box;
      }
      .nav li {
        display: inline-block;
        padding: 0 20px;
      }
      .nav li:hover {
        cursor: pointer;
        color: #ffffff;
        background-color: #d6271f;
      }

      .content {
        height: 200px;
        border: 1px solid #c8cac7;
        border-top: none;
        box-sizing: border-box;
      }
      .content .item {
        display: none;
      }
      .content .current {
        display: block;
      }
    </style>
    <script src="lib/jQuery-v3.6.0.min.js"></script>
  </head>
  <body>
    <div class="tab-box">
      <ul class="nav">
        <li>商品介绍</li>
        <li>规格与包装</li>
        <li>售后保障</li>
        <li>商品评价(50000)</li>
        <li>手机社区</li>
      </ul>
      <div class="content">
        <div class="item current">商品介绍</div>
        <div class="item">规格与包装</div>
        <div class="item">售后保障</div>
        <div class="item">商品评价(50000)</div>
        <div class="item">手机社区</div>
      </div>
    </div>

    <script>
      $(function () {
        $('.nav li').click(function () {
          // 1、点击 li，当前 li 添加 current 类，其余兄弟移除类
          $(this).addClass('current')
          $(this).siblings().removeClass('current')

          // 2、点击的同时，获取当前 li 的索引号
          var index = $(this).index()

          // 3、让对应的 item 显示，其余 item 隐藏
          $('.content .item').eq(index).show()
          $('.content .item').eq(index).siblings().hide()
        })
      })
    </script>
  </body>
</html>
